<?php

namespace addons\member\model;

/**
 * sms code 
 *
 * @author shilinqing
 */
class VericodeModel extends \web\common\model\BaseModel {

    protected function _initialize() {
        $this->tableName = 'vericode';
    }

    /**
     * verify the sms code
     * @param type $code  
     * @param type $phone  
     * @param type $type 
     * @return type
     */
    public function VerifyCode($code, $phone, $type = 1, $country_code = '86') {
        $sql = 'select * from ' . $this->getTableName() . ' where type=' . $type . ' and code=\'' . $code . '\' and phone=\'' . $phone . '\' and pass_time > \'' . NOW_DATETIME . '\' and count <= 4 and country_code='.$country_code;
        return $this->query($sql);
    }

    /**
     * Verify whether the sms is overdue by type
     * @param type $phone
     * @param type $type 1=register , 2=change login password , 3=change payment password
     */
    public function hasUnpassCode($phone, $type = 1, $country_code = '86') {
        $where['phone'] = $phone;
        $where['type'] = $type;
        $where['country_code'] = $country_code;
        $where['count'] = array('<', 5);
        $where['pass_time'] = array('>', getTime());
        return $this->where($where)->find();
    }

    /*
     * 获取当天同一手机号获取短信次数
     */
    public function getTodaySmsCount($phone)
    {
        $maps['phone'] = $phone;
        $maps['pass_time'] = ['between', [date("Y-m-d"), date("Y-m-d") . ' 23:59:59']];
        return $this->where($maps)->count();

    }

    public function sendEmail($email){
        $emailSend = config('emailSet');
        $email_host = $emailSend['email_host'];
        $email_port = $emailSend['email_port'];
        $email_id = $emailSend['email_id'];
        $email_pass = $emailSend['email_pass'];
        $email_addr = $emailSend['email_addr'];

        $vericode = rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
        $subject = '注册验证通知';
        $subject = 'Registration verification notification';
        $site_title = '抖信';
        $send_time = NOW_DATETIME;
         $message = "【{$site_title}】您于{$send_time}提交注册验证，验证码是：{$vericode}。";
//        $message = "【{$site_title}】 you submitted the registration verification on {$send_time}, and the verification code is {$vericode}.";
        $obj_email = new \web\common\utils\Email();
        $obj_email->set('email_server',$email_host);
        $obj_email->set('email_port',$email_port);
        $obj_email->set('email_user',$email_id);
        $obj_email->set('email_password',$email_pass);
        $obj_email->set('email_from',$email_addr);
        $obj_email->set('site_name',$site_title);
        $result = $obj_email->send($email,$subject,$message);
        $return['success'] = false;
        if ($result === false){
            $return['message'] = lang('API_CODE_FAIL');//发送邮箱失败';
            return $return;
        }else {
            $return['success'] = true;
            $return['message'] = lang('API_EMAIL_SENT_SUCCESSFULLY');//"验证码发送成功，请注意查收";
            $return['code'] = $vericode;
            return $return;
        }
    }

}
